import treeToArray from '../utils/eval'
import TreeUtil from '@/libs/tree'
import Vue from 'vue'
export default {
    props: {
        expandAll: {
            type: Boolean,
            default: false
        }
    },
    computed: {
        // 格式化数据源
        formatData: function() {
            var tree = TreeUtil.arrayTransTree(this.data, 'pid', 'children');
            let tmp
            if (!Array.isArray(tree)) {
                tmp = [tree]
            } else {
                tmp = tree
            }
            const func = this.evalFunc || treeToArray
            const args = this.evalArgs ? Array.concat([tmp, this.expandAll], this.evalArgs) : [tmp, this.expandAll]
            return func.apply(null, args)
        },
    },
    methods: {
        // 切换下级是否展开
        toggleExpanded: function(trIndex) {
            const record = this.formatData[trIndex]
            record._expanded = !record._expanded
        },
        showRow: function(row) {
            const show = (row.row.parent ? (row.row.parent._expanded && row.row.parent._show) : true)
            row.row._show = show
            return show ? 'animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;' : 'display:none;'

        },
    }
}
